import router from './router'
import store from './store'
import {MessageBox} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'// progress bar style
// import { getToken } from '@/utils/auth' // getToken from cookie

NProgress.configure({showSpinner: false})// NProgress Configuration

const whiteList = ['Login', 'NotFound']// 白名单，不需要验证

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  /*
   * 只能添加一次
   * */
  if (whiteList.indexOf(to.name) >= 0) {
    next();
  } else {
    if (!store.state.app.userInfo) {
      store.dispatch('getUserInfo')
        .then(res => {
            store.dispatch('GenerateRoutes',res.data.pages)
            .then((res) => {
              router.addRoutes(store.getters.addRouters)
              next({ ...to, replace: true })
              NProgress.done()
            })
        })
        .catch(error => {
          NProgress.done()
        })
    } else {
      next();
      NProgress.done()
    }
  }
})

router.afterEach((to) => {
  NProgress.done() // finish progress bar
})

